package com.hhb.controller;

import com.hhb.constant.Result;
import com.hhb.constant.UploadImgConstant;
import com.hhb.util.MinioUtils;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@RequestMapping("/api")
@CrossOrigin
public class MinioUploadController {
    @Resource
    MinioUtils minioUtils;
    @Resource
    StringRedisTemplate stringRedisTemplate;
    @PostMapping("/upload")
    public Result uploadFile(@RequestParam MultipartFile file){
        String filename = UUID.randomUUID().toString().substring(0,5);
        minioUtils.upload(file,filename);
        String fileUrl = minioUtils.getFileUrl(filename);
        stringRedisTemplate.opsForSet().add(UploadImgConstant.allUploadImgs,filename);
        return new Result(200,"上传成功",fileUrl);
    }
}
